package com.lee.mao.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.lee.mao.R;

import java.util.List;

public class TextAdapter extends RecyclerView.Adapter<TextAdapter.TextViewHolder> {

    private List<String> txts;

    private Context ctx;

    public TextAdapter(List<String> txts, Context ctx) {
        this.txts = txts;
        this.ctx = ctx;
    }

    @NonNull
    @Override
    public TextViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

        View inflate = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_txt, viewGroup, false);
        TextViewHolder holder = new TextViewHolder(inflate);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull TextViewHolder holder, @SuppressLint("RecyclerView") int i) {

        holder.tx.setText(txts.get(i));
        holder.tx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (textItemClick != null)
                    textItemClick.onclick(i);
            }
        });
    }


    TextItemClick textItemClick;

    public void setTextItemClick(TextItemClick textItemClick) {
        this.textItemClick = textItemClick;
    }

    public interface TextItemClick {

        void onclick(int position);
    }


    @Override
    public int getItemCount() {

        if (txts == null)
            return 0;

        return txts.size();
    }

    public class TextViewHolder extends RecyclerView.ViewHolder {


        TextView tx;

        public TextViewHolder(@NonNull View itemView) {
            super(itemView);

            tx = itemView.findViewById(R.id.text);
        }


    }
}
